import os
from collections.abc import Sequence
from dataclasses import dataclass
from operator import itemgetter
from typing import override

from ..utils import SheetColumnsExtractor
from .interface import Unmarshaler

__all__ = ["Document", "DocumentStage", "DocumentUnmarshaler"]

type Document = str


@dataclass(frozen=True)
class DocumentStage(object):
    documents: Sequence[Document]


class DocumentUnmarshaler(Unmarshaler[DocumentStage]):
    """
    Unmarshaler for document data.

    This unmarshaler is used to unmarshal a single column from a sheet. The returned value is a `DocumentStage`.
    """

    _extractor: SheetColumnsExtractor

    def __init__(self, *, path: os.PathLike, column: str) -> None:
        self._extractor = SheetColumnsExtractor(path=path, columns=(column,))

    @override
    def unmarshal(self) -> DocumentStage:
        return DocumentStage(list(map(itemgetter(0), self._extractor.extract())))
